﻿using System;
using System.IO;
using vm.Aspects.Properties;

namespace vm.Aspects.Diagnostics
{
    /// <summary>
    /// DumpUtilities
    /// </summary>
    public static class DumpUtilities
    {
        /// <summary>
        /// The string representing a null value.
        /// </summary>
        public static readonly string Null = Resources.StringNull;
        /// <summary>
        /// The string representing an unknown value.
        /// </summary>
        public static readonly string Unknown = Resources.StringUnknown;

        /// <summary>
        /// Appends <paramref name="countSpaces"/> number of spaces to the current new line sequence of the <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="countSpaces">The number of spaces to append to the current new line sequence.</param>
        /// <returns>The <paramref name="writer"/> object.</returns>
        /// <exception cref="System.ArgumentNullException">If the <paramref name="writer"/> is <see langword="null"/>.</exception>
        public static TextWriter Indent(this TextWriter writer, int countSpaces = 2)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            if (countSpaces > 0)
                writer.NewLine += new string(' ', countSpaces);

            return writer;
        }

        /// <summary>
        /// Removes <paramref name="countSpaces"/> number of spaces from the end of the current new line sequence of the <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="countSpaces">The number of spaces to append to the current new line sequence.</param>
        /// <returns>The <paramref name="writer"/> object.</returns>
        /// <exception cref="System.ArgumentNullException">If the <paramref name="writer"/> is <see langword="null"/>.</exception>
        public static TextWriter Unindent(this TextWriter writer, int countSpaces = 2)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            if (countSpaces == 0)
                return writer;

            if (countSpaces < 0)
            {
                writer.NewLine = null;
                return writer;
            }

            if (writer.NewLine.Length - countSpaces <= 2)
                writer.NewLine = null;
            else
                writer.NewLine = writer.NewLine.Substring(0, writer.NewLine.Length-countSpaces);

            return writer;
        }
    }
}
